﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Resources;

namespace ApplicationLibrary.ExtendedControls
{
    public static class EnumsListCreator
    {
        public static object CreateItem(object a, string b)
        {
#if (SQLite)
            if (GlobalInfo.LoginInfo.ProviderType == ProviderType.SQLite)
                return new EnumsList<long>((long)(int)(a), b);
            else
#endif
                return new EnumsList<int>((int)a, b);
        }

        public static object GetValue(object enumlist)
        {
#if (SQLite)
            if (GlobalInfo.LoginInfo.ProviderType == ProviderType.SQLite)
                return ((EnumsList<long>)enumlist).Archive;
            else
#endif
                return ((EnumsList<int>)enumlist).Archive;
        }

        public static string GetText(object enumlist)
        {
#if (SQLite)
            if (GlobalInfo.LoginInfo.ProviderType == ProviderType.SQLite)
                return ((EnumsList<long>)enumlist).Display;
            else
#endif
                return ((EnumsList<int>)enumlist).Display;
        }

        public static void ChangeText(object enumlist, string text)
        {
#if (SQLite)
            if (GlobalInfo.LoginInfo.ProviderType == ProviderType.SQLite)
                ((EnumsList<long>)enumlist).Display = text;
            else
#endif
                ((EnumsList<int>)enumlist).Display = text; ;
        }
    }

    public class EnumsManager<T>
    {
        private List<object> List = new List<object>();
        private System.Resources.ResourceManager myManager = null;

        private ComboBox cbbBox = null;
        private DataGridViewComboBoxColumn dgwCbbBox = null;

        public int Count
        {
            get { return List.Count; }
        }

        public EnumsManager()
            : this(null, "", false)
        {
        }

        public EnumsManager(ComboBox cbbBox)
            : this(cbbBox, "", false)
        {
        }

        public EnumsManager(ComboBox cbbBox, string resource)
            : this(cbbBox, resource, false)
        {
        }

        public EnumsManager(bool bAlsoNull)
            : this(null, "", bAlsoNull)
        {
        }

        public EnumsManager(string resource)
            : this(null, resource, false)
        {
        }

        public EnumsManager(DataGridViewComboBoxColumn cbbBox)
            : this(null, "", false)
        {
            AttachTo(cbbBox);
        }

        public EnumsManager(DataGridViewComboBoxColumn cbbBox, string resource)
            : this(null, resource, false)
        {
            AttachTo(cbbBox);
        }

        public EnumsManager(ComboBox cbbBox, string resource, bool bAlsoNull)
        {
            // Creates the ResourceManager.
            if (resource != string.Empty)
                myManager = new System.Resources.ResourceManager(resource, System.Reflection.Assembly.GetAssembly(typeof(T)));

            FillArray(bAlsoNull);

            if (cbbBox != null)
                AttachTo(cbbBox);
        }

        public EnumsManager(ComboBox cbbBox, ResourceManager resource, bool bAlsoNull)
        {
            myManager = resource;
            FillArray(bAlsoNull);

            if (cbbBox != null)
                AttachTo(cbbBox);
        }

        public EnumsManager(ResourceManager resource)
            : this(null, resource, false)
        {

        }

        #region MyRegion

        private void FillArray(bool bAlsoNull)
        {
            if (bAlsoNull)
                List.Add(new EnumsList<T>(default(T), ""));

            foreach (T i in Enum.GetValues(typeof(T)))
            {
                object a = (object)Enum.Parse(typeof(T), i.ToString());

                if (DisplayValue(i))
                    List.Add(EnumsListCreator.CreateItem(a, Tranlate(Enum.GetName(typeof(T), i))));
            }
        }

        private string Tranlate(string sText)
        {
            string text = string.Empty;
            if (myManager == null)
                return sText;

            try
            {
                text = myManager.GetString(sText);
                System.Diagnostics.Debug.Assert(text != null);
            }
            catch (Exception e)
            {
                text = e.Message;
                System.Diagnostics.Debug.Assert(false);
            }

            return text;
        }

        #endregion

        public void Refresh()
        {
            if (cbbBox != null && cbbBox.BindingContext[List] != null)
                ((CurrencyManager)cbbBox.BindingContext[List]).Refresh();
        }

        public void AttachTo(ComboBox cbbBox)
        {
            this.cbbBox = cbbBox;

            cbbBox.DataSource = List;
            cbbBox.DisplayMember = "Display";
            cbbBox.ValueMember = "Archive";
        }

        public void AttachTo(DataGridViewComboBoxColumn cbbBox)
        {
            this.dgwCbbBox = cbbBox;

            dgwCbbBox.DataSource = List;
            dgwCbbBox.DisplayMember = "Display";
            dgwCbbBox.ValueMember = "Archive";
        }

        public virtual bool DisplayValue(T t)
        {
            return true;
        }

        public T GetValue()
        {
            if (cbbBox != null)
            {
                if (cbbBox.SelectedValue == null || cbbBox.SelectedValue == System.DBNull.Value)
                    return (T)Enum.ToObject(typeof(T), 0);
                int pp = (int)Enum.Parse(typeof(T), cbbBox.SelectedValue.ToString());
                return (T)Enum.Parse(typeof(T), cbbBox.SelectedValue.ToString());
            }
            else
            {
                return (T)Enum.ToObject(typeof(T), 0);
                //if (dgwCbbBox.v == null)
                //    return 0;
                //int pp = (int)Enum.Parse(enumType, dgwCbbBox.t.ToString());
                //return Enum.Parse(enumType, dgwCbbBox.SelectedValue.ToString());
            }
        }

        public T GetValue(object selectedValue)
        {
            if (selectedValue == null || selectedValue is System.DBNull)
                return (T)Enum.ToObject(typeof(T), 0);
            string text = Tranlate(((string)selectedValue));

            if (Enum.IsDefined(typeof(T), text))
                return (T)Enum.Parse(typeof(T), text);
            else
            {
                Type undertype = Enum.GetUnderlyingType(typeof(T));
                return (T)Enum.ToObject(typeof(T), 0);
            }
        }

        public string GetText(int pos)
        {
            return EnumsListCreator.GetText(List[pos]);
        }

        public object GetValue(int pos)
        {
            return EnumsListCreator.GetValue(List[pos]);
        }

        //public int GetIntValue()
        //{
        //    if (cbbBox != null && cbbBox.SelectedValue != null)
        //        return (int)((EnumsList)List[cbbBox.SelectedIndex]).Archive;
        //    else
        //        return 0;
        //}

        public void ChangeText(int val, string Text)
        {
            EnumsListCreator.ChangeText(List[val], Text);
        }
    }

    public class EnumsList<L>
    {
        private L myArchive;
        private string mydisplay;

        public EnumsList(L archive, string display)
        {
            this.myArchive = archive;
            this.mydisplay = display;
        }

        public L Archive { get { return myArchive; } }

        public string Display
        {
            get { return mydisplay; }
            set { mydisplay = value; }
        }

        public override string ToString()
        {
            return this.myArchive.ToString();
        }
    }
}